package com.example.travelservlet.like.service;

import com.example.travelservlet.entertainment.repository.EntertainmentRepository;
import com.example.travelservlet.entity.Entertainment;
import com.example.travelservlet.entity.Like;
import com.example.travelservlet.entity.LikeDto;
import com.example.travelservlet.entity.User;
import com.example.travelservlet.like.dao.LikeMapper;
import com.example.travelservlet.user.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author hxy
 * @date 2023/5/10 9:05
 */
@Service
public class LikeService {
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private EntertainmentRepository entertainmentRepository;

    public Integer insertLike(Like like){
        return likeMapper.insertLike(like);
    }

    public Integer deleteLike(String userId,String id){
        return likeMapper.deleteLike(userId,id);
    }

    public List<LikeDto> findLikeByUserIdAndType(String userId, Integer type) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("type",type);
        List<Like> likeList = likeMapper.findLikeByUserIdAndType(map);

        List<LikeDto> likeDtoList = new ArrayList<>();
        for (Like like : likeList){
            LikeDto likeDto = new LikeDto();
            Optional<User> optional = userRepository.findById(like.getUserId());
            User user = optional.get();
            likeDto.setUser(user);
            if(like.getType()==1){
                Optional<Entertainment> o = entertainmentRepository.findById(like.getId());
                Entertainment entertainment = o.get();
                likeDto.setObject(entertainment);
            }else {

            }
            BeanUtils.copyProperties(like,likeDto);
            likeDtoList.add(likeDto);
        }
        return likeDtoList;
    }
}
